$OpenBSD: patch-src_options_c,v 1.1 2017/01/05 13:50:49 tb Exp $

Fix heap overflow.
https://github.com/ggreer/the_silver_searcher/pull/1040/

--- src/options.c.orig	Sat Dec  3 22:54:24 2016
+++ src/options.c	Tue Jan  3 22:16:05 2017
@@ -199,6 +199,7 @@ void parse_options(int argc, char **argv, char **base_
     int ch;
     size_t i;
     int path_len = 0;
+    int base_path_len = 0;
     int useless = 0;
     int group = 1;
     int help = 0;
@@ -775,6 +776,7 @@ void parse_options(int argc, char **argv, char **base_
     }
 
     char *path = NULL;
+    char *base_path = NULL;
 #ifdef PATH_MAX
     char *tmp = NULL;
 #endif
@@ -792,10 +794,20 @@ void parse_options(int argc, char **argv, char **base_
             (*paths)[i] = path;
 #ifdef PATH_MAX
             tmp = ag_malloc(PATH_MAX);
-            (*base_paths)[i] = realpath(path, tmp);
+            base_path = realpath(path, tmp);
 #else
-            (*base_paths)[i] = realpath(path, NULL);
+            base_path = realpath(path, NULL);
 #endif
+            if (base_path) {
+                base_path_len = strlen(base_path);
+                /* add trailing slash */
+                if (base_path_len > 1 && base_path[base_path_len - 1] != '/') {
+                    base_path = ag_realloc(base_path, base_path_len + 2);
+                    base_path[base_path_len] = '/';
+                    base_path[base_path_len + 1] = '\0';
+                }
+            }
+            (*base_paths)[i] = base_path;
         }
         /* Make sure we search these paths instead of stdin. */
         opts.search_stream = 0;
